//
//  UIImage+Format.m
//  玩翻天
//
//  Created by Bell on 15/5/21.
//  Copyright (c) 2015年 DeYi. All rights reserved.
//

#import "UIImage+Format.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIImage (Format)

- (UIImage *)scaleToScale:(float)scale
{
    UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
    [self drawInRect:CGRectMake(0, 0, self.size.width * scale, self.size.height * scale)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return scaledImage;
}


- (UIImage *)reSizeToSize:(CGSize)size

{
    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}

- (UIImage *)reSizeToMaxSize:(CGSize)maxSize
{
    CGSize imageSize = self.size;
    CGFloat widthScale = maxSize.width / imageSize.width;
    CGFloat heightScale = maxSize.height / imageSize.height;
    if (widthScale >= 1) {
        if (heightScale >= 1) {
            return self;
        } else {
            return [self scaleToScale:heightScale];
        }
    } else {
        if (heightScale >= 1) {
            return [self scaleToScale:widthScale];
        } else {
            return [self scaleToScale: heightScale < widthScale ? heightScale : widthScale];
        }
    }
}


- (UIImage *)reSizeToMinSize:(CGSize)minSize
{
    CGSize imageSize = self.size;
    CGFloat widthScale = minSize.width / imageSize.width;
    CGFloat heightScale = minSize.height / imageSize.height;
    if (widthScale <= 1) {
        if (heightScale <= 1) {
            return self;
        } else {
            return [self scaleToScale:heightScale];
        }
    } else {
        if (heightScale <= 1) {
            return [self scaleToScale:widthScale];
        } else {
            return [self scaleToScale: heightScale > widthScale ? heightScale : widthScale];
        }
    }
}

- (UIImage*)imageWithMaxLength:(NSUInteger)maxLength
{
    NSData *data = UIImageJPEGRepresentation(self,1);
    NSUInteger length = data.length;
    
    if (length > maxLength) {   // 压缩图片大小
        CGFloat rate = (CGFloat)maxLength / (CGFloat)length * 0.7;
        while (length > maxLength && rate > 0) {
            data = UIImageJPEGRepresentation(self,rate);
            length = data.length;
            rate -= 0.2;
            NSLog(@"length:%ld",(long)length);
        }
    } else {
        return self;
    }
    
    if (length > maxLength) { // 压缩图片size
        UIImage *image = [UIImage imageWithData:data];
        CGFloat scale = (CGFloat)maxLength / (CGFloat)length * 0.7;
        while (length > maxLength && scale) {
            image = [image scaleToScale:scale];
            data = UIImageJPEGRepresentation(image,0.7);
            length = data.length;
            scale = (CGFloat)maxLength / (CGFloat)length * 0.7;
            NSLog(@"length:%ld",(long)length);
        }
        return image;
    } else {
        return [UIImage imageWithData:data];
    }
}

- (NSData *)dataWithMaxLength:(NSUInteger)maxLength
{
    NSData *data = UIImageJPEGRepresentation(self,1);
    NSUInteger length = data.length;
    
    if (length > maxLength) {   // 压缩图片大小
        CGFloat rate = (CGFloat)maxLength / (CGFloat)length * 0.7;
        while (length > maxLength && rate > 0) {
            data = UIImageJPEGRepresentation(self,rate);
            length = data.length;
            rate -= 0.2;
            NSLog(@"length:%ld",(long)length);
        }
    }

    if (length > maxLength) { // 压缩图片size
        UIImage *image = [UIImage imageWithData:data];
        CGFloat scale = (CGFloat)maxLength / (CGFloat)length * 0.7;
        while (length > maxLength && scale) {
            image = [image scaleToScale:scale];
            data = UIImageJPEGRepresentation(image,0.7);
            length = data.length;
            scale = (CGFloat)maxLength / (CGFloat)length * 0.7;
            NSLog(@"length:%ld",(long)length);
        }
    }
    return data;
}

@end
